﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager
{

    #region 单例
    public static AudioManager I
    {
        get
        {
            if (mI == null)
                mI = new AudioManager();

            return mI;
        }
    }
    private AudioManager() { }
    private static AudioManager mI;

    #endregion

    private List<AudioSource> mAudioSourceList = new List<AudioSource>();
    private Dictionary<string, AudioClip> mClipDic = new Dictionary<string, AudioClip>();
    private List<AudioData> mAudioDataList = new List<AudioData>();
    private GameObject mAudioParent;


    public void Init()
    {
        mClipDic.Clear();
        mAudioDataList.Clear();
        if (mAudioParent != null)
            GameObject.DestroyImmediate(mAudioParent);

        string path = "Audio/";
        //mClipDic.Add(DefineChar.AdShoot1, (AudioClip)Resources.Load(path + DefineChar.AdShoot1, typeof(AudioClip)));
        //mClipDic.Add(DefineChar.AdShoot2, (AudioClip)Resources.Load(path + DefineChar.AdShoot2, typeof(AudioClip)));
        //mClipDic.Add(DefineChar.AdBackground, (AudioClip)Resources.Load(path + DefineChar.AdBackground, typeof(AudioClip)));
        //mClipDic.Add(DefineChar.AdGetCopper, (AudioClip)Resources.Load(path + DefineChar.AdGetCopper, typeof(AudioClip)));

        Object[] oList = Resources.LoadAll(path, typeof(AudioClip));
        for (int i = 0; i < oList.Length; i++)
        {
            mClipDic.Add(oList[i].name, (AudioClip)oList[i]);
        }



        mAudioParent = new GameObject();
        mAudioParent.gameObject.name = "AudioParent";
        GameObject.DontDestroyOnLoad(mAudioParent);


        for (int i = 0; i < 15; i++)
        {
            CreateAudioSource();
        }
    }

    public void PlayAudio(string guid, bool isLoop = false)
    {
        AudioData source = GetAudioCompoent();
        AudioClip ac = null;
        if (mClipDic.ContainsKey(guid))
            ac = mClipDic[guid];
        else
            return;

        source.AS.clip = ac;
        source.AS.loop = isLoop;
        source.AS.Play();
    }

    public void StopAllAudio()
    {
        for (int i = 0; i < mAudioDataList.Count; i++)
        {
            if (mAudioDataList[i].AS.isPlaying)
                mAudioDataList[i].AS.Stop();
        }
    }

    public void StopAudio(string clipName)
    {
        for (int i = 0; i < mAudioDataList.Count; i++)
        {
            if (mAudioDataList[i].AS.isPlaying && mAudioDataList[i].AS.clip.name == clipName)
            {
                mAudioDataList[i].AS.Stop();
                break;
            }
        }
    }



    private AudioData GetAudioCompoent()
    {
        for (int i = 0; i < mAudioDataList.Count; i++)
        {
            if (mAudioDataList[i].AS.isPlaying == false)
                return mAudioDataList[i];
        }

        return CreateAudioSource();
    }

    public AudioData CreateAudioSource()
    {
        GameObject go = new GameObject();
        AudioSource audioSource = go.AddComponent<AudioSource>();
        go.name = "audio" + mAudioDataList.Count;
        go.transform.SetParent(mAudioParent.transform);
        AudioData result = new AudioData()
        {
            AS = audioSource
        };

        mAudioDataList.Add(result);

        return result;

    }



}
public class AudioData
{
    public AudioSource AS { get; set; }
}
